package top.drinyeit.nongmao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout;

import java.lang.ref.WeakReference;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.app.Constant;
import top.drinyeit.nongmao.base.BaseActivity;
import top.drinyeit.nongmao.model.CategoryBean;
import top.drinyeit.nongmao.presenter.ShopInnerGoodsPresenter;
import top.drinyeit.nongmao.util.SPUtil;

/**
 * Created by wu on 2018/1/27.
 */

public class ShopInnerGoodsActivity extends BaseActivity {

    public CategoryBean category;
    public TextView deleteAll, pay, money, number, goodsNumber,tv_store_detail;
    public RelativeLayout bottomCart, cartView;
    public LinearLayout cart;
    private View outside;
    private RelativeLayout bg;
    public RecyclerView mRecyclerView, cartRecyclerView, groupRecycler;
    public SHSwipeRefreshLayout shSwipeRefreshLayout;
    private RelativeLayout categoryCart;
    public int category_id;
    private String categoryName;
    public mHandler mHandler = new mHandler(this);
    private boolean cartShow = false;

    public ShopInnerGoodsPresenter secondCategoryNewPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_inner_goods);

        initView();
        initIntent();
        initData();
    }

    public void initView() {
        bg = findViewById(R.id.rl_bg);
        groupRecycler = findViewById(R.id.rv_group);
        mRecyclerView = findViewById(R.id.mRecyclerView);
        cartRecyclerView = findViewById(R.id.cartRecyclerView);
        shSwipeRefreshLayout = findViewById(R.id.mShswipeRefreshLayout);
        categoryCart = findViewById(R.id.rl_bottom_cart);
        cart = findViewById(R.id.ll_cart);
        deleteAll = findViewById(R.id.tv_delete_all);
        outside = findViewById(R.id.v_outside);
        pay = findViewById(R.id.tv_category_pay);
        money = findViewById(R.id.tv_category_money);
        number = findViewById(R.id.tv_category_number);
        cartView = findViewById(R.id.rl_cart_rv);
        goodsNumber = findViewById(R.id.tv_goods_number);
        tv_store_detail=findViewById(R.id.tv_store_detail);

        initPopupWindow();
    }

    private void initPopupWindow() {

        categoryCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation animationEnter = AnimationUtils.loadAnimation(v.getContext(), R.anim.bottom_menu_enter);
                Animation animationExit = AnimationUtils.loadAnimation(v.getContext(), R.anim.bottom_menu_exit);

                animation(animationEnter);
                animation(animationExit);

                if (!cartShow) {
                    bg.setVisibility(View.VISIBLE);
                    secondCategoryNewPresenter.getCartList();
                    cart.setAnimation(animationEnter);
                    cart.setVisibility(View.VISIBLE);
                    cartShow = true;
                } else {
                    bg.setVisibility(View.GONE);
                    cart.setAnimation(animationExit);
                    cart.setVisibility(View.GONE);
                    cartShow = false;
                }
            }
        });

        cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation animationExit = AnimationUtils.loadAnimation(v.getContext(), R.anim.bottom_menu_exit);
                animation(animationExit);
                bg.setVisibility(View.GONE);
                cart.setAnimation(animationExit);
                cart.setVisibility(View.GONE);
                cartShow = false;
            }
        });
        deleteAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                secondCategoryNewPresenter.deleteAll();
            }
        });
        pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SPUtil.getShareString(Constant.USER_ID).equals("")) {
                    showToast("请先登录");
                    return;
                }
                secondCategoryNewPresenter.pay();
            }
        });
        tv_store_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(ShopInnerGoodsActivity.this,StoreInfoActivity.class).putExtra("store_id",1));
            }
        });
    }

    private void animation(Animation animationExit) {
        animationExit.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                categoryCart.setClickable(false);
                cart.setClickable(false);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                categoryCart.setClickable(true);
                cart.setClickable(true);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    public void initIntent() {
//        category = (CategoryBean) getIntent().getExtras().get("category");
//        category_id = category.getId();
//        categoryName = category.getCategory_name();
        setTitle(true, categoryName);
        LinearLayout ll_top = findViewById(R.id.ll_top);
        ImageView iv_back = findViewById(R.id.iv_back);
        ll_top.setBackgroundColor(getResources().getColor(R.color.app_green));
        iv_back.setImageResource(R.drawable.back_icon);
        TextView tv_title = findViewById(R.id.tv_title);
        tv_title.setTextColor(getResources().getColor(R.color.white));
    }

    public void initData() {
        secondCategoryNewPresenter = new ShopInnerGoodsPresenter(this);
        secondCategoryNewPresenter.getCartList();
    }


    static class mHandler extends Handler {
        WeakReference<ShopInnerGoodsActivity> activityWeakReference;

        mHandler(ShopInnerGoodsActivity activity) {
            activityWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            ShopInnerGoodsActivity activity = activityWeakReference.get();


            switch (msg.what) {
                case 0x123:
                    if (activity != null) {
                        activity.secondCategoryNewPresenter.getCartList();
                    }
                    break;
            }
        }
    }

    ;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            switch (resultCode) {
                case 1:
                    secondCategoryNewPresenter.getCartList();
                    break;
            }
        }
    }
}
